package org.falak.logic;

import java.io.IOException;

import org.falak.data.DataBaseHelper;

import android.content.Context;

public class FindPhoneNosInfo {

	private Context context = null;;
	private DataBaseHelper data = null;

	public FindPhoneNosInfo(Context contextGot) {
		this.context = contextGot;
	}

	public boolean isIndian(String phoneNumber) {
		// If standard numbering system is used then we hit this
		if (phoneNumber.length() == 13
				&& phoneNumber.substring(0, 3).equalsIgnoreCase("+91")) {
			return true;
		}

		// 000800 toll free catch
		if (phoneNumber.length() == 13
				&& phoneNumber.substring(0, 3).equalsIgnoreCase("000")) {
			return true;
		}

		// If storing the number or getting CID without the +ISD will hit this
		if (phoneNumber.length() == 10 && phoneNumber.indexOf("+") == -1) {
			return true;
		}

		// If storing the number of getting CID without the +ISD will hit this
		if (phoneNumber.length() == 11 && phoneNumber.indexOf("+") == -1) {
			return true;
		}

		return false;
	}

	public String getIndianInfo(String phoneNumber) {
		String addIndianInfo = null;
		if (checkIfTollFree(phoneNumber)) {
			addIndianInfo = "INDIA \nTOLL FREE \nCONFERENCE CALL";
		} else if (checkIsMobile(phoneNumber)) {
			addIndianInfo = getMobileOperatorState(phoneNumber);
		} else {
			addIndianInfo = "LANDLINE " + getLandLineInfo(phoneNumber);
		}

		return addIndianInfo;
	}

	private boolean checkIfTollFree(String phoneNumber) {
		if (phoneNumber.length() == 13
				&& phoneNumber.substring(0, 3).equalsIgnoreCase("000")) {
			return true;
		}
		return false;
	}

	private String getLandLineInfo(String phoneNumber) {
		// Send phone to DB as we might need to make multiple calls
		String location = this.getData().getIndiaLandlineInfo(phoneNumber);

		if (location != null) {
			return location + "\nCOUNTRY: INDIA";
		} else {
			return "INDIA LANDLINE: NOT IN MY DB !";
		}
	}

	private String getMobileOperatorState(String phoneNumber) {
		String code = null;
		if (phoneNumber.length() == 13) {
			code = phoneNumber.substring(3, 7);
		} else {
			code = phoneNumber.substring(0, 4);
		}

		String location = this.getData().getLocOperator(code);
		/*
		 * Test Code if (code.equalsIgnoreCase("7760")) { return
		 * "Airtel, Karnataka"; } if (code.equalsIgnoreCase("9886")) { return
		 * "Vodafone, Karnataka"; }
		 */

		if (location != null) {
			return location + "\nCOUNTRY: INDIA";
		} else {
			return "INDIA MOBILE: UNKNOWN OPERATOR !";
		}
	}

	private boolean checkIsMobile(String phoneNumber) {
		String code = null;
		if (phoneNumber.length() == 13) {
			code = phoneNumber.substring(3, 7);
		} else if (phoneNumber.length() == 10) {
			code = phoneNumber.substring(0, 4);
		} else {
			return false;
		}

		if (this.getData().checkIsNumberMobile(code)) {
			return true;
		}
		return false;
	}

	public String getIntlInfo(String phoneNumber) {
		// Just handle 1 or 2 digit international code as of now
		String intlInfo = null;
		String code = null;
		if (phoneNumber.length() == 13) {
			// 2 digit ISD code
			code = phoneNumber.substring(0, 3);
		} else if (phoneNumber.length() == 12) {
			// 1 digit ISD code
			code = phoneNumber.substring(0, 2);
		} else {
			return "NO ISD CODE PRESENT!";
		}

		intlInfo = this.getData().getIntlLocation(code);

		/*
		 * Test Code if (code.equalsIgnoreCase("7760")) { return
		 * "Airtel, Karnataka"; } if (code.equalsIgnoreCase("9886")) { return
		 * "Vodafone, Karnataka"; }
		 */

		if (intlInfo != null) {
			return "COUNTRY: " + intlInfo;
		} else {
			return "COUNTRY: NOT IN MY DB !";
		}
	}

	public Context getContext() {
		return this.context;
	}

	public DataBaseHelper getData() {
		if (this.data == null) {
			this.data = new DataBaseHelper(this.getContext());
			try {
				this.data.createDataBase();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (this.data != null) {
					this.getData().close();
				}
			}
		}
		return data;
	}
}
